package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.FactoryData;
import com.ruoyi.factorydata.domain.ProductionLineData;
import com.ruoyi.factorydata.domain.WorkshopData;
import com.ruoyi.factorydata.domain.dto.ProductionLineDataDTO;
import com.ruoyi.factorydata.domain.vo.ProductionLineDataVo;
import com.ruoyi.factorydata.mapper.FactoryDataMapper;
import com.ruoyi.factorydata.mapper.ProductionLineDataMapper;
import com.ruoyi.factorydata.mapper.StationDataMapper;
import com.ruoyi.factorydata.mapper.WorkshopDataMapper;
import com.ruoyi.factorydata.service.IProductionLineDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生产线数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@Service
public class ProductionLineDataServiceImpl extends ServiceImpl<ProductionLineDataMapper, ProductionLineData> implements IProductionLineDataService {

    @Autowired
    private ProductionLineDataMapper productionLineDataMapper;

    @Autowired
    private StationDataMapper stationDataMapper;

    @Autowired
    private FactoryDataMapper factoryDataMapper;

    @Autowired
    private WorkshopDataMapper workshopDataMapper;
    /**
     * 查询生产线数据
     *
     * @param id 生产线数据主键
     * @return 生产线数据
     */
    @Override
    public ProductionLineData selectProductionLineDataById(Long id) {
        return productionLineDataMapper.selectProductionLineDataById(id);
    }

    /**
     * 查询生产线数据列表
     *
     * @param productionLineDataDTO 生产线数据
     * @return 生产线数据
     */
    @Override
    public List<ProductionLineDataVo> selectProductionLineDataList(ProductionLineDataDTO productionLineDataDTO) {
//        StationData stationData = new StationData();
//        if (productionLineData != null && productionLineData.getId() != null){
//            stationData.setProductionLineId(productionLineData.getId());
//        }
//        List<StationData> stationDataList = stationDataMapper.selectStationDataList(stationData);
//        List<ProductionLineDataVo> productionLineDataVoList = stationDataList.stream().map(station -> {
//            ProductionLineData productionLineDataById = productionLineDataMapper.selectProductionLineDataById(station.getProductionLineId());
//            WorkshopData workshopDataById = workshopDataMapper.selectWorkshopDataById(productionLineDataById.getWorkshopId());
//            FactoryData factoryDataById = factoryDataMapper.selectFactoryDataById(workshopDataById.getFactoryId());
//            return ProductionLineDataVo.builder()
//                    .id(productionLineDataById.getId())
//                    .name(productionLineDataById.getName())
//                    .productionLineCode(productionLineDataById.getProductionLineCode())
//                    .productionLineId(productionLineDataById.getId())
//                    .workshopId(workshopDataById.getId())
//                    .workshopName(workshopDataById.getName())
//                    .factoryName(factoryDataById.getFactoryName())
//                    .factoryId(factoryDataById.getId())
//                    .remarks(productionLineDataById.getRemarks())
//                    .delFlag(productionLineDataById.getDelFlag())
//                    .version(productionLineDataById.getVersion())
//                    .stationId(station.getId())
//                    .stationName(station.getName())
//                    .stationCode(station.getStationCode())
//                    .createBy(productionLineDataById.getCreateBy())
//                    .createTime(productionLineDataById.getCreateTime())
//                    .updateTime(productionLineDataById.getUpdateTime());
//        }).collect(Collectors.toList()).stream().map(ProductionLineDataVo.ProductionLineDataVoBuilder::build).collect(Collectors.toList());
        ProductionLineData productionLineData = new ProductionLineData();
        productionLineData.setWorkshopId(productionLineDataDTO.getWorkshopId());
        productionLineData.setName(productionLineDataDTO.getName());
        productionLineData.setProductionLineCode(productionLineDataDTO.getProductionLineCode());
        List<ProductionLineData> productionLineDataList = productionLineDataMapper.selectProductionLineDataList(productionLineData);
        List<ProductionLineDataVo> productionLineDataVoList = productionLineDataList.stream().map(product -> {
            WorkshopData workshopData = workshopDataMapper.selectWorkshopDataById(product.getWorkshopId());
            FactoryData factoryData = factoryDataMapper.selectFactoryDataById(workshopData.getFactoryId());
            return ProductionLineDataVo.builder()
                    .id(product.getId())
                    .name(product.getName())
                    .productionLineCode(product.getProductionLineCode())
                    .workshopId(workshopData.getId())
                    .workshopName(workshopData.getName())
                    .factoryId(workshopData.getFactoryId())
                    .factoryName(factoryData.getFactoryName())
                    .remarks(product.getRemarks())
                    .delFlag(product.getDelFlag())
                    .version(product.getVersion())
                    .createBy(product.getCreateBy())
                    .createTime(product.getCreateTime())
                    .updateTime(product.getUpdateTime());
        }).map(ProductionLineDataVo.ProductionLineDataVoBuilder::build).collect(Collectors.toList());
        Page<ProductionLineData> queryData=(Page<ProductionLineData>)productionLineDataList;
        Page<ProductionLineDataVo> page = new Page<>(queryData.getPageNum(), queryData.getPageSize());
        page.setTotal(queryData.getTotal());
        page.addAll(productionLineDataVoList);
        return page;
    }

    /**
     * 新增生产线数据
     *
     * @param productionLineData 生产线数据
     * @return 结果
     */
    @Override
    public int insertProductionLineData(ProductionLineData productionLineData) {
                productionLineData.setCreateTime(DateUtils.getNowDate());
            return productionLineDataMapper.insertProductionLineData(productionLineData);
    }

    /**
     * 修改生产线数据
     *
     * @param productionLineData 生产线数据
     * @return 结果
     */
    @Override
    public int updateProductionLineData(ProductionLineData productionLineData) {
                productionLineData.setUpdateTime(DateUtils.getNowDate());
        return productionLineDataMapper.updateProductionLineData(productionLineData);
    }

    /**
     * 批量删除生产线数据
     *
     * @param ids 需要删除的生产线数据主键
     * @return 结果
     */
    @Override
    public int deleteProductionLineDataByIds(Long[] ids) {
        int i = productionLineDataMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除生产线数据信息
     *
     * @param id 生产线数据主键
     * @return 结果
     */
    @Override
    public int deleteProductionLineDataById(Long id) {
        return productionLineDataMapper.deleteProductionLineDataById(id);
    }
}